package com.zhengke.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.common.object.Result;
import com.zhengke.common.util.FileExport;
import com.zhengke.dao.bbfx.ServicePointMapper;
import com.zhengke.service.IServicePoint;
@Service
public class ServicePointImpl implements IServicePoint {

	@Autowired
	private ServicePointMapper dao;

	@Override
	public Result findPoint(String startDate, String endDate,int page,int limit) {
		Map<String, Object> paramap = new HashMap<String, Object>();
		paramap.put("startDate",startDate);
		paramap.put("endDate",endDate);
		
	PageHelper.startPage(page,limit,true);
		List<Map<String, Object>> rtnList = dao.findPoint(paramap);
	
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(rtnList);
		Result rs = new Result();
		rs.setCode("0");
		rs.setCount(pageInfo.getTotal());
		rs.setData(rtnList);
		rs.setMsg("success");
		return rs;
	}

	@Override
	public Result findRunaccount(String startDate, String endDate, int page, int limit) {

		Map<String, Object> paramap = new HashMap<String, Object>();
		paramap.put("startDate",startDate);
		paramap.put("endDate",endDate);
		
		PageHelper.startPage(page,limit,true);
		List<Map<String, Object>> rtnList = dao.findRunaccount(paramap);
	
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(rtnList);
		Result rs = new Result();
		rs.setCode("0");
		rs.setCount(pageInfo.getTotal());
		rs.setData(rtnList);
		rs.setMsg("success");
		return rs;
		
	}
	
	/**
	 * 导出excel
	 * @param response
	 */
	@RequestMapping("export")
	public void exportExcel(String startDate,String endDate,HttpServletResponse response) {
		Map<String, Object> paramap = new HashMap<String, Object>();
		paramap.put("startDate",startDate);
		paramap.put("endDate",endDate);
		try {
	List<Map<String, Object>> listrtn=dao.findRunaccount(paramap);
			String[] topName = {"日期","服务点名称","收入金额","订单编号","用户TVN号","用户姓名"};
			List<Object[]> list = new ArrayList<Object[]>();
			for(Map<String, Object> map:listrtn) {
				Object[] obj = new Object[topName.length];
				obj[0]=map.get("addData");
				obj[1]=map.get("area_name");
				obj[2]=map.get("rech_amount");
				obj[3]=map.get("order_sn");
				obj[4]=map.get("user_id");
				obj[5]=map.get("user_name");
				list.add(obj);
			}
			FileExport.exportExcel(topName, list, "服务点流水账信息", response);
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	

}
